//
//  MainTabBar.swift
//  微博
//
//  Created by MI on 15/11/9.
//  Copyright © 2015年 young_Mi. All rights reserved.
//

import UIKit

class MainTabBar: UITabBar {
    //默认的构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    //重写init(frame:) 系统默认就是代码创建
    required init?(coder aDecoder: NSCoder) {
        //如果调用Xib .sb 系统就混崩溃
        //        fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
        
        setupUI()
    }
    
    private func setupUI() {
        addSubview(composeBtn)
        
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        //遍历子视图
        let w = self.bounds.width / 5
        let h = self.bounds.height
        let rect = CGRectMake(0, 0, w, h)
        var i:CGFloat = 0
        for subView in subviews{
            if subView.isKindOfClass(NSClassFromString("UITabBarButton")!){
                
                subView.frame = CGRectOffset(rect, i * w, 0)
                i += i == 1 ? 2 :1

            }
        }
        //设置按钮的位置
        composeBtn.frame = CGRectOffset(rect, w * 2, -4)
        bringSubviewToFront(composeBtn)
        
    }
    
    //懒加载
    lazy var composeBtn: UIButton = {
        
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
        btn.sizeToFit()
        
        
        return btn
    }()
}


